package com.atguigu.guilimall.gulimallgateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 配置过滤器 跨域
 *
 * @author: Mr.ling
 * @data: 2022/5/20
 * @apiNote
 */
@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        //基于url跨域，选择reactive包下的
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        // 跨域配置信息
        CorsConfiguration corsConf = new CorsConfiguration();
        // 允许跨域的头
        corsConf.addAllowedHeader("*");
        // 允许跨域的请求方式
        corsConf.addAllowedMethod("*");
        // 允许跨域的请求来源
        corsConf.addAllowedOrigin("*");
        // 是否允许携带cookie跨域
        corsConf.setAllowCredentials(true);

        // 任意url都要进行跨域配置
        source.registerCorsConfiguration("/**", corsConf);
        return new CorsWebFilter(source);
    }
}
